package com.ruoyi.qbms.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.BaseSelect;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qbms.mapper.QbmsCategoryMapper;
import com.ruoyi.qbms.domain.QbmsCategory;
import com.ruoyi.qbms.service.IQbmsCategoryService;

/**
 * 题库类别Service业务层处理
 * 
 * @author zouhuu
 * @date 2022-10-16
 */
@Service
public class QbmsCategoryServiceImpl implements IQbmsCategoryService 
{
    @Autowired
    private QbmsCategoryMapper qbmsCategoryMapper;

    
    /**
     * 查询分类树 数据
     * @param qbmsCategory
     * @return 树格式数据
     */
    @Override
    public List<BaseSelect> selectQbmsCategoryOptions(QbmsCategory qbmsCategory) {
        // 分类数据
        List<QbmsCategory> qbmsCategories = qbmsCategoryMapper.selectQbmsCategoryOptions(qbmsCategory);

        // 构建基本分类选项
        List<BaseSelect> baseCategories = qbmsCategories
                .stream()
                .map(item -> new BaseSelect(item.getId(), item.getParentId(), item.getName(), null))
                .collect(Collectors.toList());

        // 构建两级树结构
        List<BaseSelect> twoLevelTree = TreeUtils.buildTwoLevelTree(baseCategories);

        return twoLevelTree;
    }


    /**
     * 查询题库类别
     * 
     * @param id 题库类别主键
     * @return 题库类别
     */
    @Override
    public QbmsCategory selectQbmsCategoryById(Long id)
    {
        return qbmsCategoryMapper.selectQbmsCategoryById(id);
    }

    /**
     * 查询题库类别列表
     * 
     * @param qbmsCategory 题库类别
     * @return 题库类别
     */
    @Override
    public List<QbmsCategory> selectQbmsCategoryList(QbmsCategory qbmsCategory)
    {
        return qbmsCategoryMapper.selectQbmsCategoryList(qbmsCategory);
    }

    /**
     * 新增题库类别
     * 
     * @param qbmsCategory 题库类别
     * @return 结果
     */
    @Override
    public int insertQbmsCategory(QbmsCategory qbmsCategory)
    {
        qbmsCategory.setCreateTime(DateUtils.getNowDate());
        qbmsCategory.setCreateBy(SecurityUtils.getUsername());
        qbmsCategory.setUserId(SecurityUtils.getUserId());
        qbmsCategory.setDeptId(SecurityUtils.getDeptId());
        return qbmsCategoryMapper.insertQbmsCategory(qbmsCategory);
    }

    /**
     * 修改题库类别
     * 
     * @param qbmsCategory 题库类别
     * @return 结果
     */
    @Override
    public int updateQbmsCategory(QbmsCategory qbmsCategory)
    {
        qbmsCategory.setUpdateTime(DateUtils.getNowDate());
        qbmsCategory.setUpdateBy(SecurityUtils.getUsername());
        return qbmsCategoryMapper.updateQbmsCategory(qbmsCategory);
    }

    /**
     * 批量删除题库类别
     * 
     * @param ids 需要删除的题库类别主键
     * @return 结果
     */
    @Override
    public int deleteQbmsCategoryByIds(Long[] ids)
    {
        return qbmsCategoryMapper.deleteQbmsCategoryByIds(ids);
    }

    /**
     * 删除题库类别信息
     * 
     * @param id 题库类别主键
     * @return 结果
     */
    @Override
    public int deleteQbmsCategoryById(Long id)
    {
        return qbmsCategoryMapper.deleteQbmsCategoryById(id);
    }
}
